// event SubscriptionCreated(uint256 indexed subId, address owner);

const extractEventArg = (receipt, contract, eventName, argName) => {
    const iface = contract.interface;
    const results = [];

    for (const log of receipt.logs) {
        try {
            const parsedLog = iface.parseLog(log);
            console.log(`Event emitted: ${parsedLog.name}`, parsedLog.args);
            if (parsedLog.name === eventName) {
                results.push(parsedLog.args[argName]);
                // If we only want the first event, we can return immediately
                // return parsedLog.args[argName];
            }
        } catch (err) {
            // Ignore errors for logs that are not related to the event we are looking for
            // console.log("Error parsing log:", err);
            continue;
        }
    }

    if (results.length === 0) {
        throw new Error(`Event "${eventName}" with argument "${argName}" not found in logs.`);
    }
    
    return results;
}

module.exports = { extractEventArg }
